﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;
using brand.cyt369.com.models;

namespace brand.cyt369.com.dal
{
    public class BrandService : brand.cyt369.com.dal.DbContexts.BrandBaseService<BrandService>
    {
        public BrandService() { }

        /// <summary>
        /// 查询品牌信息
        /// </summary>
        /// <param name="brandId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public brand.cyt369.com.models.Brand GetBrandByID(long brandId)
        {
            using (var ctx = this.BrandCtx)
            {
                var brand = (from o in ctx.Brands
                             .Include(c => c.BrandImages)
                             .Include(c=>c.Category)
                             .ThenInclude(c=>c.Parent)
                                where o.Id == brandId && o.ReviewResult==1 && o.IsDeleted == false
                                select o).FirstOrDefault();
                return brand;
            }
        }

        /// <summary>
        /// 获取商品扩展信息
        /// </summary>
        /// <param name="brandId"></param>
        /// <returns></returns>
        public brand.cyt369.com.models.BrandExtension GetBrandExtension(int brandId)
        {
            using (var ctx = this.BrandCtx)
            {
                var brandExtension = (from o in ctx.BrandExtensions
                             where o.BrandID == brandId
                             select o).FirstOrDefault();
                return brandExtension;
            }
        }

        /// <summary>
        /// 获取品牌排行前10
        /// </summary>
        /// <returns></returns>
        public IQueryable<brand.cyt369.com.models.Brand> BrandTop10Gets()
        {
            using (var ctx = this.BrandCtx)
            {
                var brandTop10 = (from o in ctx.Brands
                                  where o.ReviewResult == 1 && o.IsDeleted == false
                                  select o).OrderBy(c => c.InquiryCount).Take(10);
                return brandTop10.ToList().AsQueryable();
            }
        }

        /// <summary>
        /// 获取品牌通过父类别ID
        /// </summary>
        /// <param name="categoryParentID"></param>
        /// <returns></returns>
        public IQueryable<brand.cyt369.com.models.Brand> GetBrandsByCategoryParentID(int categoryParentID)
        {
            using (var ctx = this.BrandCtx)
            {
                var brands=ctx.Brands.Include(b=>b.Category).Include(b=>b.BrandImages)
                    .Where(b=>b.ReviewResult==1 && b.IsDeleted == false && b.Category.ParentID== categoryParentID)
                    .OrderBy(c=>c.EditDate)
                    .Take(5);
                return brands.ToList().AsQueryable();
            }
        }


        /// <summary>
        /// 获取所有的品牌
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="total"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public IQueryable<brand.cyt369.com.models.Brand> GetBrands(int page, int rows, ref int total,string searchStr = null,int? categoryID = null,int? amount=null,int ? cityID=null)
        {
            using (BrandCtx)
            {
                var items = BrandCtx.Brands
                    .Include(b => b.BrandImages)
                    .Include(cb => cb.Category)
                    .ThenInclude(c=>c.Parent)
                    .Where(b=>b.IsDeleted==false)
                    .AsQueryable();

                if (categoryID.HasValue&&categoryID!=0)
                {
                    items = items.Where(i=>i.Category.Id==categoryID||i.Category.ParentID==categoryID);
                }
                if (amount.HasValue&&amount!=0)
                {
                    items = items.Where(i => i.Amount == amount);
                }
                if (!string.IsNullOrEmpty(searchStr))
                {
                    items = items.Where(c =>c.Name!=null&&c.Name.Contains(searchStr.Trim()));
                }
                if (cityID.HasValue&&cityID!=0)
                {
                    items = items.Where(i => i.FranchiseRegionID == cityID);
                }
                return items = this.Pagination(page, rows, ref total, items, c => c.Id);
            }
        }

        /// <summary>
        /// 获取所有的品牌 不包含图片
        /// </summary>
        /// <param name="total"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public IQueryable<brand.cyt369.com.models.Brand> GetBrands_NoIncludeImg(string searchStr = null, int? categoryID = null, int? amount = null, int? cityID = null)
        {
            using (BrandCtx)
            {
                var items = BrandCtx.Brands
                    .Include(cb => cb.Category)
                    .ThenInclude(c => c.Parent)
                    .Where(b => b.IsDeleted == false)
                    .AsQueryable();

                if (categoryID.HasValue && categoryID != 0)
                {
                    items = items.Where(i => i.Category.Id == categoryID || i.Category.ParentID == categoryID);
                }
                if (amount.HasValue && amount != 0)
                {
                    items = items.Where(i => i.Amount == amount);
                }
                if (!string.IsNullOrEmpty(searchStr))
                {
                    items = items.Where(c => c.Name != null && c.Name.Contains(searchStr.Trim()));
                }
                if (cityID.HasValue && cityID != 0)
                {
                    items = items.Where(i => i.FranchiseRegionID == cityID);
                }
                return items;
            }
        }
    }
}
